package com.tangdi.Action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.tangdi.Utils;
import com.tangdi.Bean.CZGZ_DWCOUNT;
import com.tangdi.Bean.CZGZ_DWXX;
import com.tangdi.Bean.CZGZ_DWXX_TOTAL;
import com.tangdi.Bean.CZGZ_GZLB;
import com.tangdi.Bean.CZGZ_SJGZ;
import com.tangdi.Bean.DW_DM_MC;
import com.tangdi.Bean.XM_BM_XM_MC;
import com.tangdi.Dao.impl.GongziDWImpl;
import com.tangdi.Dao.impl.GongziMXImpl;
import com.tangdi.Dao.impl.SystemParameterImpl;
import com.tangdi.mkfile.bank_bankcc.CreateBankCCTxt;
import com.tangdi.mkfile.bank_bankcc.CreateBankTxt;
import com.tangdi.mkfile.gzdwhz.CreateDWGZExcel_LISTALL;
import com.tangdi.mkfile.gzdwhz.CreateDWGZPDF_LISTALL;
import com.tangdi.mkfile.gzdwmx.CreateDWGZMXExcel_OneByOne;
import com.tangdi.mkfile.gzdwmx.CreateDWGZMXPDF_OneByOne;
import com.tangdi.mkfile.gzdwmx.CreateDWGZMXPDF_TOTAL;
import com.tangdi.mkfile.gzdwtzs.CreateDWGZExcel_TZS;
import com.tangdi.mkfile.gzdwtzs.CreateDWGZPDF_TZS;
import com.tangdi.mkfile.gzffmx.CreateGZFFMXExcel_ByDW;
import com.tangdi.mkfile.gzffmx.CreateGZFFMXPDF_ByDW;
import com.tangdi.mkfile.gzffmx.CreateGZFFMXPDF_ListAll;

public class GZ_Report_CreateAction_bak  extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(GZ_Report_CreateAction_bak.class);

	public GZ_Report_CreateAction_bak() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}
	
	public void init() throws ServletException {
		// Put your code here
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		String type = request.getParameter("type");
		String year_month =request.getParameter("year_month");
		String return_result = "生成报表成功！";
		int cr_status = -1;
		//报表类型
		String[] report_Name = {"DW_HZ",
				                "DW_MX",
				                "DW_TZS",
				                "GZ_MX",
				                "BANK_REPORT"
				                };
		HttpSession session=request.getSession();
		if(year_month != null && type.equals("2")){
			//年月
			String month = year_month;
			//获取所需数据 start ---------------------------------------------------------------------
			//工资类别，科目项信息
			Map<String,List<XM_BM_XM_MC>> map_bm_mc_list = new HashMap<String,List<XM_BM_XM_MC>>();
			//工资类别list
			List<CZGZ_GZLB> listlb=new ArrayList<CZGZ_GZLB>();
			GongziMXImpl gzlb = new GongziMXImpl();
			listlb = gzlb.listGZLBReport(month);	
			//实际工资科目 按工资类别
			List<CZGZ_SJGZ> list_sjgz=new ArrayList<CZGZ_SJGZ>();
			GongziMXImpl sjgz = new GongziMXImpl();
			list_sjgz = sjgz.listSJGZReport(month);	
			
			//组装工资类别 ，实际工资科目项，对应好关系；方便后续使用
			for(int i=0;i<listlb.size();i++){
				List<XM_BM_XM_MC> bm_mc_list = new ArrayList<XM_BM_XM_MC>();
				System.out.println(listlb.get(i).getLB_BM().trim());
				for(int j=0; j<list_sjgz.size(); j++){
					XM_BM_XM_MC bm_mc= new XM_BM_XM_MC();
					if((listlb.get(i).getLB_BM().trim()).equals(list_sjgz.get(j).getGZ_LB().trim())){
						bm_mc.setXM_BM(Utils.getXM_BM(list_sjgz.get(j).getXM_BM().trim()));
						bm_mc.setXM_MC(list_sjgz.get(j).getXM_MC().trim());
						bm_mc_list.add(bm_mc);
					}
				}
				map_bm_mc_list.put(listlb.get(i).getLB_BM().trim(), bm_mc_list);
			}

			//根据单位编号、工资类别统计工资列表
			List<CZGZ_DWCOUNT> listDWCOUNT=new ArrayList<CZGZ_DWCOUNT>();
			GongziMXImpl gz_dw_count = new GongziMXImpl();
			listDWCOUNT = gz_dw_count.listGZDWCount(month);
			//获取所需数据 end ---------------------------------------------------------------------
			if(map_bm_mc_list.size() == 0 || listlb.size() == 0 || list_sjgz.size() == 0 || listDWCOUNT.size() == 0){
				return_result = "请确认数据是否完整！";
			}
			else{
				SystemParameterImpl sp_info = new SystemParameterImpl();
				String is_PrintTogether = "0";
				is_PrintTogether = sp_info.getIs_PrintTogetherMark("IS_PRINT_TOGETHER_MARK");
			    //报表制作  start ---------------------------------------------------------------------
				for(int s = 0; s < report_Name.length; s++){
					if(report_Name[s].equals("DW_HZ")){
						logger.info("-------------------------单位工资汇总报表  Pdf制作 start-------------------------");
						//单位工资信息list
						List<CZGZ_DWXX> listDW=new ArrayList<CZGZ_DWXX>();
						GongziDWImpl gzDW = new GongziDWImpl();
						listDW = gzDW.listGZDWReport(month);			
						//单位工资汇总
						CZGZ_DWXX_TOTAL dwCount=new CZGZ_DWXX_TOTAL();
						GongziDWImpl gzDWcount = new GongziDWImpl();
						dwCount = gzDWcount.getGZDWCount(month);

						CreateDWGZPDF_LISTALL cPdf = new CreateDWGZPDF_LISTALL();
						cr_status = cPdf.createPDF(listDW, dwCount, month);
						logger.info("-------------------------单位工资汇总报表  Pdf制作完成 end-------------------------");
					    if(cr_status != 0){
					    	return_result = "单位工资汇总报表Pdf生成失败！";
					    	break;
					    }

					    logger.info("-------------------------单位工资汇总报表 Excel 制作 start-------------------------");
						CreateDWGZExcel_LISTALL cExcel = new CreateDWGZExcel_LISTALL();
						cr_status = cExcel.createExcel(listDW, dwCount, month);
					    logger.info("-------------------------单位工资汇总报表 Excel 制作 end-------------------------");
					    if(cr_status != 0){
					    	return_result = "单位工资汇总报表Excel生成失败！";
					    	break;
					    }
					    
					}else if(report_Name[s].equals("DW_MX")){
					    logger.info("-------------------------单位工资发放明细表 PDF制作 start-------------------------");
						CreateDWGZMXPDF_OneByOne cPdf = new CreateDWGZMXPDF_OneByOne();
						cr_status = cPdf.createPDF(map_bm_mc_list,listDWCOUNT,listlb,month);
					    logger.info("-------------------------单位工资发放明细表 PDF制作 end-------------------------");
					    if(cr_status != 0){
					    	return_result = "单位工资发放明细表PDF生成失败！";
					    	break;
					    }

					    logger.info("-------------------------单位工资发放明细表 Excel制作 start-------------------------");
						CreateDWGZMXExcel_OneByOne cExcel = new CreateDWGZMXExcel_OneByOne();
						cr_status = cExcel.createExcel(map_bm_mc_list,listDWCOUNT,listlb,month);
					    logger.info("-------------------------单位工资发放明细表 Excel制作 end-------------------------");
					    if(cr_status != 0){
					    	return_result = "单位工资发放明细表Excel生成失败！";
					    	break;
					    }
					    
					    if("1".equals(is_PrintTogether)){
						    logger.info("-------------------------all单位工资发放明细表 PDF制作  start-------------------------");
						    CreateDWGZMXPDF_TOTAL cPDF_all = new CreateDWGZMXPDF_TOTAL();
							cr_status = cPDF_all.createPDF(map_bm_mc_list, listDWCOUNT, listlb, month);
						    logger.info("-------------------------all单位工资发放明细表 PDF制作 end-------------------------");
						    if(cr_status != 0){
						    	return_result = "all单位工资发放明细表 PDF生成失败！";
						    	break;
						    }
					    }
					}
					else if(report_Name[s].equals("DW_TZS")){
						//根据单位编号
					    logger.info("-------------------------单位转账通知书 PDF制作 start-------------------------");
						List<DW_DM_MC> listDW_DM_MC=new ArrayList<DW_DM_MC>();
						GongziMXImpl gz_DW_List = new GongziMXImpl();
						listDW_DM_MC = gz_DW_List.getListDW_DM_MC(month);
						
						CreateDWGZPDF_TZS cPdf = new CreateDWGZPDF_TZS();
						cr_status = cPdf.createPDF(map_bm_mc_list, listDW_DM_MC, listlb, month);
					    logger.info("-------------------------单位转账通知书 PDF制作 end-------------------------");
					    if(cr_status != 0){
					    	return_result = "单位转账通知书PDF生成失败！";
					    	break;
					    }

					    logger.info("-------------------------单位转账通知书 Excel制作start-------------------------");
					    CreateDWGZExcel_TZS cExcel = new CreateDWGZExcel_TZS();
					    cExcel.createExcel(map_bm_mc_list, listDW_DM_MC, listlb, month);
					    logger.info("-------------------------单位转账通知书 Excel制作 end-------------------------");
					    if(cr_status != 0){
					    	return_result = "单位转账通知书Excel生成失败！";
					    	break;
					    }
					}else if(report_Name[s].equals("GZ_MX")){
					    logger.info("-------------------------按单位生成工资条 PDF制作 start-------------------------");
						CreateGZFFMXPDF_ByDW cPDF_bydw = new CreateGZFFMXPDF_ByDW();
						cr_status = cPDF_bydw.createPDF(map_bm_mc_list, listDWCOUNT, listlb, month);
					    logger.info("-------------------------按单位生成工资条 PDF制作 end-------------------------");
					    if(cr_status != 0){
					    	return_result = "按单位生成工资条PDF失败！";
					    	break;
					    }
					    
					    logger.info("-------------------------按单位生成工资条 Excel制作 start-------------------------");
					    CreateGZFFMXExcel_ByDW cExcel = new CreateGZFFMXExcel_ByDW();
						cr_status = cExcel.createExcel(map_bm_mc_list, listDWCOUNT, listlb, month);
					    logger.info("-------------------------按单位生成工资条 Excel制作 end-------------------------");
					    if(cr_status != 0){
					    	return_result = "all工资条PDF生成失败！";
					    	break;
					    }

					    if("1".equals(is_PrintTogether)){
						    logger.info("-------------------------all工资条 PDF制作 start-------------------------");
							CreateGZFFMXPDF_ListAll cPDF_all = new CreateGZFFMXPDF_ListAll();
							cr_status = cPDF_all.createPDF(map_bm_mc_list, listDWCOUNT, listlb, month);
						    logger.info("-------------------------all工资条 PDF制作 end-------------------------");
						    if(cr_status != 0){
						    	return_result = "all工资条PDF生成失败！";
						    	break;
						    }
					    }
					}else if(report_Name[s].equals("BANK_REPORT")){
						logger.info("------------------------- Bank txt文件 制作 start-------------------------");
						CreateBankTxt bank = new CreateBankTxt();
						cr_status = bank.writeFile(month);
					    logger.info("------------------------- Bank txt文件 制作 end-------------------------");
					    if(cr_status != 0){
					    	return_result = "Bank txt文件 生成失败！";
					    	break;
					    }

					    logger.info("------------------------- BankCC txt文件 制作 start-------------------------");
					    CreateBankCCTxt bankCC = new CreateBankCCTxt();
					    cr_status = bankCC.writeFile(month);
					    logger.info("------------------------- BankCC txt文件 制作 end-------------------------");
					    if(cr_status != 0){
					    	return_result = "BankCC txt文件 生成失败！";
					    	break;
					    }
					}
				}
			  //报表制作  end --------------------------------------------------------------------- 
				
			}
			session.setAttribute("cr_status", String.valueOf(cr_status));
			session.setAttribute("return_result", return_result);
			session.setAttribute("year_month", year_month);	
		}else{
			session.setAttribute("cr_status", null);
			session.setAttribute("return_result", null);
			session.setAttribute("year_month", null);			
		}
		
		response.sendRedirect("gz_report_create.jsp");
		
	}
}